<template>
  <!-- // 轮播图 -->
  <view class="search-box">
    <my-search @click="gotoSearch"></my-search>
  </view>
  <view>
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular="true">
      <swiper-item v-for="item in swiperList" :key="item.goods_id">
        <navigator class="swiper-item" :url="`/subpkg/good_detail/good_detail?goods_id=${item.goods_id}`">
          <image :src=item.image_src alt=""  />
          </navigator>
      </swiper-item>
    </swiper>
  </view>
  <!-- //分类导航 -->
  <view class="nav">
    <view class="nav-item" v-for="item in navList" :key="item.name" @click="navClickHandler(item)">
      <image :src="item.image_src" mode=""></image>
    </view>
  </view>
  <!-- 楼层 -->
  <view class="floor" v-for="(item,index) in floorList" :key="index">
    <image :src="item.floor_title.image_src" mode="heightFix"></image>
    <view class="floor-box"> 
      <navigator class="left-img" :url=item.product_list[0].url>
        <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
      </navigator>
      <view class="right-img-box" >
        <navigator class="right-img" v-for="(item2, i2) in item.product_list" :key="i2" :url="item2.url">
          <image :src="item2.image_src"  :style="{width: item2.image_width + 'rpx'}" v-if="i2 !== 0" mode="widthFix"></image>
        </navigator>
      </view>
    </view>
  </view>
</template>

<script>
  import $http from '@/utils/request.js'
  import { useCartStore } from '../../store/store';
  const cartStore = useCartStore()
  export default {
    data() {
      return {
        // 请求轮播图数据
        swiperList:[],
        // 分类导航数据
        navList: [],
        // 楼层数据
        floorList:[],
      }; 
    },
    methods: {
      // 获取轮播图
      async getSwiperData(){
        const res = await uni.$http.get('api/public/v1/home/swiperdata')
        this.swiperList = res.data.message
      },
      // 获取分类导航数据
      async getNavData() {
        const res = await uni.$http.get('api/public/v1/home/catitems')
        this.navList = res.data.message
      },
      navClickHandler(item) {
        if (item.name === '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      // 获取楼层数据
      async getFloorData() {
        const res = await uni.$http.get('api/public/v1/home/floordata')
        res.data.message.forEach(item => {
          item.product_list.forEach(prop => {
            prop.url= '/subpkg/goods_list/goods_list?' + prop.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.data.message
      },
      gotoSearch(){}
    },
    onLoad() {
      this.getSwiperData()
      this.getNavData()
      this.getFloorData()
    },
    onShow() {
      cartStore.setBage()
    }
  }
</script>

<style lang="scss" scoped>
  swiper {
    height: 330rpx;
    navigator,
    image {
      height: 100%;
      width: 100%;
    }
  }
  .nav {
    display: flex;
    justify-content: space-around;
    margin: 15rpx 0;
    image{
      width: 128rpx;
      height: 140rpx;
    }
  }
  .floor {
    padding-left: 10rpx;
    >image {
      height: 60rpx;
      width: 100%;
    }
    .floor-box{
      display: flex;
      .right-img-box{
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
      }
    }
  }
  .search-box{
    position: sticky;
    top: 0;
    z-index: 999;
  }
</style>
